package com.mpc.manager;

import javax.servlet.http.HttpServletRequest;

import com.mpc.adapter.LanguagesAdapter;
import com.mpc.beans.TabResources;
import com.mpc.params.MainTabPageParams;
import com.mpc.utils.Log;

public class UriManager {
    private final String TAG = UriManager.class.getSimpleName();

    public final static String HTML_SUFFIX = ".htm";
    public final static String TMP = "/tmp";
    public final static String PATH_2_WEBAPPS = "D:/Download_MY/Software/developer/eclipse-jee-kepler-SR2-win32/tomcat/apache-tomcat-7.0.54/webapps";

    private static UriManager sManager = null;

    private UriManager() {

    }

    public static UriManager getInstance() {
        if (sManager == null) {
            sManager = new UriManager();
        }
        return sManager;
    }

    public boolean isValidHtmlUri(HttpServletRequest request) {
        String htmUri = request.getRequestURI();
        if (htmUri == null || htmUri.length() == 0
                || -1 == htmUri.indexOf(".htm")) {
            Log.e(TAG, "invalidate html uri " + htmUri);
            return false;
        }

        if (!htmUri.startsWith(request.getServletContext().getContextPath()
                + "/" + PageManager.NAME_PAGE_HOME)
                && !htmUri.startsWith(request.getServletContext()
                        .getContextPath() + "/" + PageManager.NAME_PAGE_APPS)
                && !htmUri.startsWith(request.getServletContext()
                        .getContextPath() + "/" + PageManager.NAME_PAGE_CONACT)) {
            Log.e(TAG, "invalidate html uri " + htmUri);
            return false;
        }

        return true;
    }

    public boolean isRequestWelcomePage(HttpServletRequest request) {
        String uri = request.getRequestURI();
        String contextPath = request.getServletContext().getContextPath();

        if (uri != null
                && (uri.equals(contextPath) || uri.substring(0,
                        uri.length() - 1).equals(contextPath))) {
            return true;
        } else {
            return false;
        }
    }

    public String getUriFromRequest(HttpServletRequest request, int resources,
            boolean simple) {
        String uri = null;
        String base = null;
        if (simple) {
            base = request.getContextPath();
        } else {
            base = request.getScheme() + "://" + request.getRemoteHost() + ":"
                    + request.getRemotePort() + "/" + request.getContextPath();
        }

        switch (resources) {
        case PageManager.INDEX_PAGE_HOME:
            uri = base + PageManager.JSP_PATH_PAGE_HOME;
            break;
        case PageManager.INDEX_PAGE_APPS:
            uri = base + PageManager.JSP_PATH_PAGE_APPS;
            break;
        case PageManager.INDEX_PAGE_CONTACT:
            uri = base + PageManager.JSP_PATH_PAGE_CONTACT;
            break;
        }

        return uri;
    }

    private TabResources getLanResFromWelcomeRequest(
            HttpServletRequest request) {
        Object obj = request.getSession().getAttribute(
                LanguagesAdapter.LANGUAGES);
        if (obj == null) {
            return null;
        } else {
            return (TabResources) obj;
        }
    }

    // /mpc/home_en.htm
    public String getQueryFromHtmlUri(HttpServletRequest request) {
        String uri = request.getRequestURI();
        if (uri == null || uri.length() == 0) {
            Log.d(TAG, "request URI " + uri + " is wrong !");
            return null;
        }

        String pageHome = request.getServletContext().getContextPath() + "/"
                + PageManager.NAME_PAGE_HOME + "_";
        String pageApps = request.getServletContext().getContextPath() + "/"
                + PageManager.NAME_PAGE_APPS + "_";
        String pageContact = request.getServletContext().getContextPath() + "/"
                + PageManager.NAME_PAGE_CONACT + "_";

        String query = null;
        if (uri.startsWith(pageHome)) {
            int s = uri.indexOf(pageHome) + pageHome.length();
            int e = uri.indexOf(UriManager.HTML_SUFFIX);
            query = uri.substring(s, e);
            return query;
        } else if (uri.startsWith(pageApps)) {
            int s = uri.indexOf(pageApps) + pageApps.length();
            int e = uri.indexOf(UriManager.HTML_SUFFIX);
            query = uri.substring(s, e);
            return query;
        } else if (uri.startsWith(pageContact)) {
            int s = uri.indexOf(pageContact) + pageContact.length();
            int e = uri.indexOf(UriManager.HTML_SUFFIX);
            query = uri.substring(s, e);
            return query;
        } else {
            Log.d(TAG, "request URI " + uri + " is wrong !");
            return null;
        }
    }

    public String path2HomeHtmlStorage(HttpServletRequest request) {
        TabResources res = getLanResFromWelcomeRequest(request);
        if (res == null) {
            Log.d(TAG, "request not for welcome page");
            return null;
        }

        String context = request.getServletContext().getContextPath();
        String pageHome = PageManager.JSP_PATH_PAGE_HOME.replace(".jsp", "_");

        return context + TMP + pageHome + res.getLanguages() + HTML_SUFFIX;
    }

    public String path2HomeJspStorage(HttpServletRequest request) {
        TabResources res = getLanResFromWelcomeRequest(request);
        if (res == null) {
            Log.d(TAG, "request not for welcome page");
            return null;
        }

        String context = request.getServletContext().getContextPath();
        String pageHome = PageManager.JSP_PATH_PAGE_HOME + "?";
        String query = MainTabPageParams.KEY_LANGUAGES + "="
                + res.getLanguages();

        return context + pageHome + query;
    }

    // /mpchome/home_en_xxx.htm
    public String path2HtmlStorage(HttpServletRequest request) {
        String uri = request.getRequestURI();
        if (uri == null || uri.length() == 0) {
            Log.d(TAG, "request uri is wrong !");
            return null;
        }

        String base = request.getServletContext().getContextPath() + TMP
                + PageManager.PAGE_DIR;
        String pageName = uri.substring(uri.lastIndexOf("/") + 1);

        return base + "/" + pageName;
    }

    // /mpchome/home_en_xxx.htm
    public String path2JspStorage(HttpServletRequest request) {
        String uri = request.getRequestURI();
        if (uri == null || uri.length() == 0) {
            Log.d(TAG, "request uri is wrong !");
            return null;
        }

        String base = request.getServletContext().getContextPath();

        int code = PageManager.getInstance().getPageCodeFromHtmlUri(request);
        if (code == -1) {
            Log.d(TAG, "request uri " + request.getRequestURI() + " is wrong !");
            return null;
        }

        String jspPath = PageManager.getInstance().getJspPathFromPageCode(code);
        if (jspPath == null) {
            Log.d(TAG, "page code " + code + " is wrong !");
            return null;
        }

        return base + jspPath;
    }
}
